import PreferencesUtil from '../common/utils/PreferencesUtil';
import { DeviceResponse,ShadowDeviceAlam,ShadowDevice, DeviceResponseAlm } from '../types/types';
import axiosDevice from './AxiosDevice'
class GetShowValues{
  private temp: string = '0'
  private hum: string = '0'
  private light_intensity: string = '0'
  private alm:string = '0'
  private getToken: string = '0'
  private shadowDevice:string = ''
  private deviceResponse: DeviceResponse = null
  private deviceResponseAlm: DeviceResponseAlm = null
  async getShadow(name: string,tk:string) {
    this.getToken= await PreferencesUtil.getPreferenceValue('MyToken', 'tokenKey', 'null')as string//从preference里获取token
    this.deviceResponse = await axiosDevice.ShowDeviceShadow(tk); //获取设备影子文件

    const shadowDevice: ShadowDevice[] = this.deviceResponse.shadow //ShadowDevice[]本地创建的数据类型
    function getUserByAttribute(attribute: keyof ShadowDevice, value: any): ShadowDevice | undefined {
      return shadowDevice.find((user) => user[attribute] === value);
    }
    const userByName = getUserByAttribute("service_id", "温湿度"); //通过服务ID查询服务下的属性
    try {
      switch (name) {
        case 'temp':
          return this.temp = JSON.stringify(userByName.reported.properties.温度);
          break;
        case 'hum':
          return this.hum = JSON.stringify(userByName.reported.properties.湿度);
          break;
        case 'light_intensity':
          return this.light_intensity = JSON.stringify(userByName.reported.properties.光照强度);
          break;
      // case 'weather':
      //   this.temp = '天气：' + JSON.stringify(userByName.reported.properties.temp);
      //   break;
      }
      console.log(`获取${name}成功.`);
    } catch (e) {
      console.log(`获取${name}失败.`);
    }
  }

//获取烟雾传感器值
  async getShadowalm(name: string,tk:string) {
    //this.getToken= await PreferencesUtil.getPreferenceValue('MyToken', 'tokenKey', 'null')as string//从preference里获取token
    this.deviceResponseAlm = await axiosDevice.ShowDeviceShadowalm(tk); //获取设备影子文件

    const shadowDeviceAlm: ShadowDeviceAlam[] = this.deviceResponseAlm.shadow //ShadowDevice[]本地创建的数据类型
    function getUserByAttribute(attribute: keyof ShadowDeviceAlam, value: any): ShadowDeviceAlam | undefined {
      return shadowDeviceAlm.find((user) => user[attribute] === value);
    }
    const userByName = getUserByAttribute("service_id", "温湿度"); //通过服务ID查询服务下的属性
    try {
      switch (name) {
        case 'alm':
          return  this.alm = JSON.stringify(userByName.reported.properties.烟雾);
          break;
      }
      console.log(`获取${name}成功.`);
    } catch (e) {
      console.log(`获取${name}失败.`);
    }
  }
}
const getShowValues = new GetShowValues();
export default getShowValues as GetShowValues